﻿using Jayden.Dll.Web.Enums;
using Jayden.Dll.Web.Events;
using Jayden.Dll.Web.HtmlControls;

namespace Jayden.Dll.Web.Controls
{
	public class SearchForm : Form
	{
		public class SearchValues
		{
			public string Term;
			public bool Ajax;
			public int SeqNo;
		}

		public delegate void OnSearch(HttpHandlerContext context, SearchValues search);

		public class SearchCommandHandler : HttpCommandHandler
		{
			public SearchCommandHandler() : this("Search") { }
			public SearchCommandHandler(string command)
				: base(command)
			{
				RegisterEvent("Search");
			}
			public event OnSearch Search { add { AddEvent<OnSearch>("Search", value); } remove { RemoveEvent<OnSearch>("Search", value); } }

			protected override void ProcessCommand(HttpHandlerContext context, string command)
			{
				SearchValues search = new SearchValues();
				search.Term = context.Request["term"];
				int seqNo = 0;
				int.TryParse(context.Request["seqNo"], out seqNo);
				search.SeqNo = seqNo;
				search.Ajax = (context.Json != null);

				Invoke("Search", context, search);
			}
		}


		private Input m_Term;
		private Button m_Search;
		private Input m_SeqNo;

		public Input Term { get { return m_Term; } }

		public override void Load(System.Xml.XmlNode node, HtmlControlLoader loader)
		{
			base.Load(node, loader);
			if (Id == null)
				Id = "searchForm";

			m_SeqNo = GetFormElement<Input>("seqNo");
			m_Term = GetFormElement<Input>("term");
			m_Search = GetFormElement<Button>("search", "command");
		}

		public override void ProcessHtmlRequest(HttpHandlerContext context)
		{
			base.ProcessHtmlRequest(context);
			SearchCommandHandler searchHandler = context.HandlerManager.GetHandler<SearchCommandHandler>();
			searchHandler.Search += new OnSearch(Search);
		}

		public override void ProcessJsonRequest(HttpHandlerContext context)
		{
			base.ProcessJsonRequest(context);
			SearchCommandHandler searchHandler = context.HandlerManager.GetHandler<SearchCommandHandler>();
			searchHandler.Search += new OnSearch(Search);
		}

		public override void ProcessJsRequest(HttpHandlerContext context)
		{
			base.ProcessJsRequest(context);
			context.Js[Id].AppendOnInit(@"
	var $term = $('#" + m_Term.Id + @"');
	var $form = $('#" + Id + @"');
	var $seqNo = $('#" + m_SeqNo.Id + @"');
	var $search = $('#" + m_Search.Id + @"');

	var seqNo = parseInt($seqNo.val());
	var lastSubmit = null;
	var submit = function() {
		if(lastSubmit === $term.val())
			return true;
		lastSubmit = $term.val();
		$seqNo.val(seqNo);
		seqNo++;
		return JAYDEN.sfn.submitAjax($form, $search);
	};
	$term.keyup(submit).change(submit);
	$form.submit(function(e) {
		e.preventDefault();
		$seqNo.val(seqNo);
		seqNo++;
		return JAYDEN.sfn.submitAjax($form, $search);
	});
");
		}

		public override void ProcessCssRequest(HttpHandlerContext context)
		{
			context.Css.NewElement("input[type=\"search\"]").AddProperty("width", "300px");
			base.ProcessCssRequest(context);
		}

		void Search(HttpHandlerContext context, SearchForm.SearchValues search)
		{
			m_Term.Value = search.Term;
			if (context.RequestType == HttpRequestType.Json)
				context.Json.Add(Id, search.Term);
		}
	}
}
